home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 February / EnigmA AMIGA RUN 15 (1997)(G.R. Edizioni)(IT)[!][issue 1997-02][PLANET CD V].iso / progs / editor / frexxed / fpl / cindent.fpl.orig < prev    next >
Text File  |  1996-07-20  |  16KB  |  606 lines

  1. // Various search strings
  2. string whitespace = "^[ \t]*";
  3. string any_character = "(\\sW|\\s!)";
  4. string left_brace_at_eol = "{[ \t]*((////.*)|//(\*).*(\*)//)?[ \t]*$";
  5. string left_or_right_brace = "(({[ \t]*((////.*)|//(\*).*(\*)//)?[ \t]*$)|(^[ \t]*}))";
  6. string case_or_default = "^[ \t]*((case[ \t]+.+)|(default[ \t]*)):";
  7. string if_or_while = "(if|while)[ \t]*\\(.*\\)[ \t]*((////.*)|//(\*).*(\*)//)?[ \t]*$";
  8. string _else = "else[ \t]*((////.*)|//(\*).*(\*)//)?[ \t]*$";
  9. string _switch = "switch[ \t]*\\(.*\\)[ \t]*((////.*)|//(\*).*(\*)//)?[ \t]*$";
  10. string backward = "=w+f-";
  11. string forward = "=w+f+";
  12.  
  13. //»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»» CModePrefs() ««
  14. void export CIndentPrefs()
  15. {
  16.    int intelligence = ReadInfo("c_intelligence");
  17.    string indentkey = ReadInfo("c_indentkey");
  18.    int usetabs = ReadInfo("c_usetabs");
  19.    int indent_level = ReadInfo("c_indent_level");
  20.    int case_level = ReadInfo("c_case_level");
  21.    int brace_offset = ReadInfo("c_brace_offset");
  22.    int cont_offset = ReadInfo("c_cont_offset");
  23.    int delay = ReadInfo("c_delay");
  24.    int face  = ReadInfo("c_face_mode");
  25.  
  26.    DeleteKey(indentkey);
  27.    
  28.    RequestWindow("C Indent Preferences", 15,
  29.       "Intelligence level", "c", &intelligence, "Stupid|Intelligent|Ninja",
  30.       "Force Indent key", "s", &indentkey,
  31.       "Use TAB character", "b", &usetabs,
  32.       "Autoenable coloured syntax", "b", &face,
  33.       "General indent level", "i", &indent_level,
  34.       "'case' indent level", "i", &case_level,
  35.       "Brace offset", "i", &brace_offset,
  36.       "Continuation offset", "i", &cont_offset,
  37.       "Parenthesis match delay", "i", &delay
  38.    );
  39.  
  40.    AssignKey("ForceIndent();", indentkey, "c_indent_mode");
  41.  
  42.    SetInfo(-1, "c_intelligence", intelligence);
  43.    SetInfo(-1, "c_indentkey", indentkey);
  44.    SetInfo(-1, "c_usetabs", usetabs);
  45.    SetInfo(-1, "c_indent_level", indent_level);
  46.    SetInfo(-1, "c_case_level", case_level);
  47.    SetInfo(-1, "c_brace_offset", brace_offset);
  48.    SetInfo(-1, "c_cont_offset", cont_offset);
  49.    SetInfo(-1, "c_delay", delay);
  50.    SetInfo(-1, "c_face_mode", face);
  51. }
  52.  
  53. int PrevLine()
  54. {
  55.    CursorStack(-1);
  56.    if(0 == Search(any_character, backward))
  57.    {
  58.       int line;
  59.  
  60.       line = ReadInfo("line");
  61.       CursorStack(1);
  62.       return(line);
  63.    }
  64.    else
  65.    {
  66.       CursorStack(1);
  67.       return(1);
  68.    }
  69. }
  70.  
  71. export void MatchIt(string paren)
  72. {
  73.    Output(paren);
  74.    CursorLeft(1);
  75.    if (MatchParen()>=0)
  76.    {
  77.       Delay(ReadInfo("c_delay"));
  78.       MatchParen();
  79.    }
  80.    else
  81.    {
  82.       ReturnStatus("No match!");
  83.    }
  84.    CursorRight(1);
  85. }
  86.  
  87. string InsertIndent(string output, int indentation)
  88. {
  89.    int x;
  90.    int tabsize = ReadInfo("tab_size");
  91.    int tabs = indentation / tabsize;
  92.    int spaces= indentation % tabsize;
  93.  
  94.    if (ReadInfo("c_usetabs"))
  95.    {
  96.       while(x < tabs)
  97.       {
  98.      output += "\t";
  99.      x++;
  100.       }
  101.       for (x = 0; x < spaces; x++)
  102.       {
  103.      output += " ";
  104.       }
  105.    }
  106.    else
  107.    {
  108.       for (x = 0; x < indentation; x++)
  109.       {
  110.      output += " ";
  111.       }
  112.    }
  113.    return (output);
  114. }
  115.  
  116. export void HashMark()
  117. {
  118.    int v = Visible(0);
  119.    int dum = Output("#");
  120.    int line = ReadInfo("line");
  121.    string thisline = GetLine();
  122.    int thislinelen = strlen(thisline) - (line != ReadInfo("lines"));
  123.    
  124.    CursorStack(-1);
  125.    GotoLine(line, 0);
  126.    CursorLeft();    /* Fix! The search cannot find if we are at
  127.                   the beginning of the line */
  128.    
  129.    if(0 <= Search("^[ \t]*\\#$",forward, thislinelen))
  130.    {
  131.       GotoLine(line, 0);
  132.       DeleteEol();
  133.       Output("#");
  134.    }
  135.    else
  136.    {
  137.       CursorStack(1);
  138.    }
  139.    Visible(v);
  140. }
  141.  
  142. export void Colon()
  143. {
  144.    int v = Visible(0);
  145.    int dum = Output(":");
  146.    int line = ReadInfo("line");
  147.    string thisline = GetLine();
  148.    int thislinelen = strlen(thisline) - (line != ReadInfo("lines"));
  149.    
  150.    CursorStack(-1);
  151.    GotoLine(line, 0);
  152.    CursorLeft();    /* Fix! The search cannot find if we are at
  153.                   the beginning of the line */
  154.    
  155.    if(0 <= Search(case_or_default, forward, thislinelen))
  156.    {
  157.       if (0 <= Search(_switch, backward))
  158.       {
  159.      int x;
  160.      int prevline = ReadInfo("line");
  161.      int linelen = ReadInfo("line_length") - (prevline != ReadInfo("lines"));
  162.      int indentation;
  163.      
  164.      GotoLine(prevline, 0);
  165.  
  166.      indentation = GetCursor(strlen(ReplaceMatch("[ \t]*", "\\&")), prevline) - 1 + ReadInfo("c_case_level");
  167.  
  168.      GotoLine(line, 0);
  169.      Replace(2, whitespace, "", forward, linelen);
  170.  
  171.      Output(InsertIndent("", indentation));
  172.      Search(":", forward);
  173.      CursorRight();
  174.       }
  175.       else
  176.       {
  177.      CursorStack(1);
  178.       }
  179.    }
  180.    else
  181.    {
  182.       CursorStack(1);
  183.    }
  184.    Visible(v);
  185. }
  186.  
  187. export void LeftBrace()
  188. {
  189.    int v = Visible(0);
  190.    int line = ReadInfo("line");
  191.    int thislinelen = ReadInfo("line_length") - (line != ReadInfo("lines"));
  192.  
  193.    Output("{");
  194.   
  195.    CursorStack(-1);
  196.    GotoLine(line, 0);
  197.    if (GetChar() != '{' && (0 <= Search("^[ \t]*{", forward, thislinelen)))
  198.    {
  199.       int x;
  200.       int prevline = PrevLine();
  201.       string input = GetLine(prevline);
  202.       int linelen = strlen(input) - (line != ReadInfo("lines"));
  203.       int indentation;
  204.       
  205.       GotoLine(prevline, 0);
  206.       indentation = GetCursor(strlen(ReplaceMatch("[ \t]*", "\\&")), prevline) - 1;
  207.  
  208.       if(0 == Search(left_brace_at_eol, forward, linelen))
  209.       {
  210.      indentation += ReadInfo("c_indent_level");
  211.       }
  212.  
  213.       GotoLine(line, 0);
  214.  
  215.       Replace(2, whitespace, "", forward, thislinelen);
  216.  
  217.       Output(InsertIndent("", indentation));
  218.       CursorRight();
  219.    }
  220.    else
  221.    {
  222.       CursorStack(1);
  223.    }
  224.    Visible(v);
  225. }
  226.  
  227. export void ForceIndent()
  228. {
  229.    int v = Visible(0);
  230.    int thisline = ReadInfo("line");
  231.    int thislinelen = ReadInfo("line_length") - (thisline != ReadInfo("lines"));
  232.    int linelen, prevline;
  233.    int indentation, intelligence;
  234.  
  235.    CursorStack(-1);
  236.    GotoLine(ReadInfo("line"), 0);
  237.    prevline = PrevLine();
  238.  
  239.    GotoLine(prevline, 0);
  240.    linelen = ReadInfo("line_length") - (ReadInfo("line") != ReadInfo("lines"));
  241.    indentation = GetCursor(strlen(ReplaceMatch("[ \t]*", "\\&")), prevline) - 1;
  242.  
  243.    if (prevline < thisline)
  244.    {
  245.       CursorStack(1);
  246.       CursorStack(-1);
  247.  
  248.       GotoLine(thisline, 0);
  249.  
  250.       if (0 <= Search(case_or_default, forward, thislinelen))
  251.       {
  252.      if (0 <= Search(_switch, backward))
  253.      {
  254.         int x;
  255.         int prevline = ReadInfo("line");
  256.         int linelen = ReadInfo("line_length") - (prevline != ReadInfo("lines"));
  257.  
  258.         GotoLine(prevline, 0);
  259.  
  260.         indentation = GetCursor(strlen(ReplaceMatch("[ \t]*", "\\&")), prevline) - 1 + ReadInfo("c_case_level");
  261.      }
  262.       }
  263.       else if (0 <= Search("^[ \t]*\\#", forward, thislinelen))
  264.       {
  265.      indentation = 0;
  266.       }
  267.       else if (GetChar() != '}' && 0 > Search("^[ \t]*}", forward, thislinelen))
  268.       {
  269.      GotoLine(prevline, 0);
  270.      if (0 == Search(left_brace_at_eol, forward, linelen))
  271.      {
  272.         indentation += ReadInfo("c_indent_level");
  273.      }
  274.      else if (intelligence = ReadInfo("c_intelligence"))
  275.      {
  276.         if (0 == Search(case_or_default, forward, linelen))
  277.         {
  278.            indentation += ReadInfo("c_indent_level");
  279.         }
  280.         else if (0 == Search(if_or_while, forward, linelen))
  281.         {
  282.            indentation += ReadInfo("c_cont_offset");
  283.         }
  284.         else if (0 == Search(_else, forward, linelen))
  285.         {
  286.            indentation += ReadInfo("c_cont_offset");
  287.         }
  288.         else if (intelligence > 1)
  289.         {
  290.            prevline = PrevLine();
  291.            GotoLine(prevline, 0);
  292.            linelen = ReadInfo("line_length");
  293.            if (0 == Search(if_or_while, forward, linelen))
  294.            {
  295.           indentation -= ReadInfo("c_cont_offset");
  296.            }
  297.            else if (0 == Search(_else, forward, linelen))
  298.            {
  299.           indentation -= ReadInfo("c_cont_offset");
  300.            }
  301.         }
  302.      }
  303.       }
  304.       else
  305.       {
  306.      int prevline;
  307.      int prevlen;
  308.      string input;
  309.  
  310.      if (ReadInfo("c_intelligence"))
  311.      {
  312.         int end, left, right;
  313.  
  314.         right = 1;
  315.         do
  316.         {
  317.            if (0 == (end = Search(left_or_right_brace, backward)))
  318.            {
  319.           if (GetChar() == '{')
  320.              left++;
  321.           else
  322.              right++;
  323.            }
  324.         }
  325.         while (!end && (left < right));
  326.  
  327.         if (left >= right)
  328.         {
  329.            prevline = ReadInfo("line");
  330.            GotoLine(prevline, 0);
  331.            indentation = GetCursor(strlen(ReplaceMatch("[ \t]*", "\\&")), prevline) - 1;
  332.         }
  333.      }
  334.       }
  335.    }
  336.  
  337.    CursorStack(1);
  338.    GotoLine(ReadInfo("line"), 0);
  339.    Replace(2, whitespace, "", forward, linelen);
  340.  
  341.    Output(InsertIndent("", indentation));
  342.    GotoLine(thisline, ReadInfo("line_length"));
  343.    Visible(v);
  344. }
  345.  
  346. export void CIndent()
  347. {
  348.    int v = Visible(0);
  349.    int linelen = Output("\n");
  350.    int prevline = PrevLine();
  351.    int indentation, intelligence;
  352.  
  353.    CursorStack(-1);
  354.    GotoLine(prevline, 0);
  355.    linelen = ReadInfo("line_length") - (ReadInfo("line") != ReadInfo("lines"));
  356.    indentation = GetCursor(strlen(ReplaceMatch("[ \t]*", "\\&")), prevline) - 1;
  357.  
  358.    CursorStack(1);
  359.    CursorStack(-1);
  360.    if (GetChar() != '}' || 0 > Search("^[ \t]*}", forward, linelen))
  361.    {
  362.       GotoLine(prevline, 0);
  363.       if (0 == Search(left_brace_at_eol, forward, linelen))
  364.       {
  365.      indentation += ReadInfo("c_indent_level");
  366.       }
  367.       else if (intelligence = ReadInfo("c_intelligence"))
  368.       {
  369.      if (0 == Search(case_or_default, forward, linelen))
  370.      {
  371.         indentation += ReadInfo("c_indent_level");
  372.      }
  373.      else if (0 == Search(if_or_while, forward, linelen))
  374.      {
  375.         indentation += ReadInfo("c_cont_offset");
  376.      }
  377.      else if (0 == Search(_else, forward, linelen))
  378.      {
  379.         indentation += ReadInfo("c_cont_offset");
  380.      }
  381.      else if (intelligence > 1)
  382.      {
  383.         prevline = PrevLine();
  384.         GotoLine(prevline, 0);
  385.         linelen = ReadInfo("line_length");
  386.         if (0 == Search(if_or_while, forward, linelen))
  387.         {
  388.            indentation -= ReadInfo("c_cont_offset");
  389.         }
  390.         else if (0 == Search(_else, forward, linelen))
  391.         {
  392.            indentation -= ReadInfo("c_cont_offset");
  393.         }
  394.      }
  395.       }
  396.    }
  397.    CursorStack(1);
  398.  
  399.    Replace(2, whitespace, "", forward, linelen);
  400.  
  401.    Output(InsertIndent("", indentation));
  402.    Visible(v);
  403. }
  404.  
  405. export void RightBrace()
  406. {
  407.    int line = ReadInfo("line");
  408.    int dum = Output("}");
  409.    int v = Visible(0);
  410.    string thisline = GetLine();
  411.    int thislinelen = strlen(thisline) - (line != ReadInfo("lines"));
  412.    string output;
  413.    int x;
  414.  
  415.    CursorStack(-1);
  416.    GotoLine(line, 0);
  417.    CursorLeft();
  418.    if (0 == Search("^[ \t]*}", forward, thislinelen))
  419.    {
  420.       int prevline;
  421.       int prevlen;
  422.       string input;
  423.  
  424.       if(ReadInfo("c_intelligence"))
  425.       {
  426.      int end, left, right;
  427.  
  428.      CursorStack(1);
  429.      CursorStack(-1);
  430.      do
  431.      {
  432.         if (0 == (end = Search(left_or_right_brace, backward)))
  433.         {
  434.            if (GetChar() == '{')
  435.           left++;
  436.            else
  437.           right++;
  438.         }
  439.      } while (!end && (left != right));
  440.      
  441.      if (end)
  442.      {
  443.         CursorStack(-1);
  444.         Visible(v);
  445.      }
  446.      else
  447.      {
  448.         int indentation;
  449.  
  450.         prevline = ReadInfo("line");
  451.         GotoLine(prevline, 0);
  452.         indentation = GetCursor(strlen(ReplaceMatch("[ \t]*", "\\&")), prevline) - 1;
  453.  
  454.         Visible(v);
  455.         Delay(ReadInfo("c_delay"));
  456.         MatchParen();
  457.         v = Visible(0);
  458.         GotoLine(line, 0);
  459.  
  460.         Replace(2, whitespace, "", forward, thislinelen);
  461.  
  462.         Output(InsertIndent("", indentation));
  463.         CursorRight();
  464.         Visible(v);
  465.      }
  466.       }
  467.       else
  468.       {
  469.      CursorStack(1);
  470.      Visible(v);
  471.       }
  472.    }
  473.    else
  474.    {
  475.       CursorStack(1);
  476.       Visible(v);
  477.    }
  478. }
  479.  
  480. export void Slash()
  481. {
  482.    int line = ReadInfo("line");
  483.    int dum = Output("/");
  484.    int pos = ReadInfo("byte_position");
  485.    int v = Visible(0);
  486.    string thisline = GetLine();
  487.    int thislinelen = strlen(thisline) - (line != ReadInfo("lines"));
  488.    string output;
  489.    int x;
  490.  
  491.    CursorStack(-1);
  492.    if(pos > 1)
  493.    {
  494.       if(thisline[pos - 2] == '*')
  495.       {
  496.      if(0 == Search("//(\*)", backward))
  497.      {
  498.         Visible(v);
  499.         Delay(ReadInfo("c_delay"));
  500.         CursorStack(1);
  501.      }
  502.       }
  503.    }
  504.    else
  505.    {
  506.       Visible(v);
  507.    }
  508. }
  509.  
  510. export void Cmode()
  511. {
  512.   if(ReadInfo("c_face_mode"))
  513.     SetInfo(-1, "face", "C");
  514.   else
  515.     SetInfo(-1, "face", "");
  516. }
  517.  
  518. /* Create C face mode */
  519. {
  520.   int face = FaceGet("C", 1); /* create one if missing */
  521.   if(face) {
  522.     int style;
  523.  
  524.     /* Get the style named "c-keywords" OR create one that is bold with
  525.        foreground pen 3 and background pen 0. If none matched and none
  526.        could be created, it will return the style of the best match and
  527.        no style will be named like this. */
  528.     style = FaceStyle("c-keywords", "bold", 1, 0);
  529.     FaceAdd(face,  /* add word(s) to this face */
  530.             style, /* use the c-keywords style */
  531.             /* Specify all words we want to add to this face with this
  532.                particular style, we can of course add more words at a
  533.                later time */
  534.             "for|do|while|else|if|int|long|short|char|extern|"
  535.             "static|auto|unsigned|signed|return|continue|break|void|"
  536.             "case|switch|WORD|LONG|struct|register|__regargs|enum|"
  537.             "float|double|goto|sizeof|union",
  538.             "word" /* these are word-only matches, that must be surrounded
  539.                       with non-word letters to get recognized */
  540.             );
  541.  
  542.     /* a bunch of the most common standard C stdio functions */
  543.     style = FaceStyle("c-stdio", "bold", 1, 0);
  544.     FaceAdd(face,  /* add word(s) to this face */
  545.             style, /* use the c-stdio style */
  546.             /* Specify all words we want to add to this face with this
  547.                particular style, we can of course add more words at a
  548.                later time */
  549.             "printf|fprintf|sprintf|"
  550.             "strlen|strcpy|strncpy|strcmp|strncmp|"
  551.             "atoi|strtol|fopen|fclose|fgets|fread|fwrite|fputs|"
  552.             "getchar|gets|getch|putc|puts|fputc|fputs",
  553.             "word" /* these are word-only matches, that must be surrounded
  554.                       with non-word letters to get recognized */
  555.             );
  556.  
  557.     style = FaceStyle("c-comments", "normal", 3, 0);
  558.     FaceAdd(face, style, "/*", "anywhere|strong", "*/");
  559.     FaceAdd(face, style, "//", "anywhere|strong|doublechk", "\n");
  560.  
  561.     style = FaceStyle("c-cpp", "normal", 2, 0);
  562.     FaceAdd(face, style, "#", "backslash|1nonspace|weak", "\n");
  563.  
  564.     style = FaceStyle("c-string", "bold", 2, 0);
  565.     FaceAdd(face, style, "\"", "backslash", "\"");
  566.  
  567.     style = FaceStyle("c-apostrophe", "normal", 2, 0);
  568.     FaceAdd(face, style, "\'", "backslash", "\'");
  569.   }
  570.   /* else
  571.     face is 0, which means GetFace() failed somehow! */
  572. }
  573.  
  574. ConstructInfo("c_indent_mode", "Cmode();", "", "LB", "", 0, 1, 0);
  575. ConstructInfo("c_indent_level", "", "", "HLIW", "", 0, 0, 3);
  576. ConstructInfo("c_brace_offset", "", "", "HLIW", "", 0, 0, 0);
  577. ConstructInfo("c_cont_offset", "", "", "HLIW", "", 0, 0, 3);
  578. ConstructInfo("c_case_level", "", "", "HLIW", "", 0, 0, 3);
  579. ConstructInfo("c_usetabs", "", "", "HLBW", "", 0, 1, 1);
  580. ConstructInfo("c_delay", "", "", "HGIW", "", 0, 0, 5);
  581. ConstructInfo("c_intelligence", "", "", "HGCW", "Stupid|Intelligent|Ninja", 0, 0, 2);
  582. ConstructInfo("c_indentkey", "", "", "HGSW", "", 0, 0, "Alt i");
  583. ConstructInfo("c_face_mode", "", "", "HGBW", "", 0, 1, 1);
  584.  
  585. AssignKey("RightBrace();", "}", "c_indent_mode");
  586. AssignKey("Output(\"\n\");", "Shift 'Return'", "c_indent_mode");
  587. AssignKey("CIndent();", "'Return'", "c_indent_mode");
  588. AssignKey("LeftBrace();", "{", "c_indent_mode");
  589. AssignKey("HashMark();", "#", "c_indent_mode");
  590. AssignKey("Colon();", ":", "c_indent_mode&c_intelligence");
  591. AssignKey("Slash();", "/", "c_indent_mode");
  592. AssignKey("ForceIndent();", ReadInfo("c_indentkey"), "c_indent_mode");
  593.  
  594. AssignKey("MatchIt(\")\");", ")", "c_indent_mode");
  595. AssignKey("MatchIt(\"]\");", "]", "c_indent_mode");
  596.  
  597. AddMode(0,"c_indent_mode", "", "");            // Add as minor mode
  598.  
  599. //»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»» CIndent menu ««
  600. MenuAdd("s", "C Indent...", "CIndentPrefs();", "",
  601.         ReadInfo("menu_program_title"),
  602.         ReadInfo("menu_program_item"),-1); // Add to PackageSettings
  603. MenuBuild();
  604.  
  605. ExecuteFile("FaceEdit.FPL"); /* To enable the users to change the styles */
  606.